//
//  PictureSelect.m
//  JiuLe
//
//  Created by 黄鑫 on 16/10/10.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "PictureSelect.h"


@interface PictureSelect()<UIActionSheetDelegate,UIImagePickerControllerDelegate>

@property(nonatomic,strong)UIViewController * rootViewController;
@property(nonatomic,strong)UIImagePickerController * photoVC;

@end

@implementation PictureSelect

- (instancetype)initWithView:(UIViewController*)viewControll{
    self.rootViewController = viewControll;
    return [super init];
}

-(void)changeIcon{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"设置头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择", nil];
    [actionSheet showInView:self.rootViewController.view];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque; 
    actionSheet.tag = 250;
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (self.photoVC==nil) {
        self.photoVC=[[UIImagePickerController alloc]init];
        self.photoVC.delegate = (id)self;
        self.photoVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        self.photoVC.allowsEditing = YES;
    }
    if (buttonIndex == 0){
        self.photoVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self.rootViewController presentViewController:self.photoVC animated:YES completion:^{
            
        }];
    }else if(buttonIndex == 1){//调用相册
        self.photoVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self.rootViewController presentViewController:self.photoVC animated:YES completion:^{
        }];
    }else{
        [actionSheet dismissWithClickedButtonIndex:2 animated:NO];
    }
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)aImage editingInfo:(NSDictionary *)editingInfo{
    [picker dismissViewControllerAnimated:YES completion:^(void){
        [self.delegate iconChooseFinish:aImage];
    }];
}


@end
